package com.twschool.practice.guessnumber;

import java.util.Scanner;

public class Game {
    private Answer answer;
    private int gameTimes;
    private GameStatus status;


    public Game(Answer answer){
        this.answer = answer;
        init();
    }

    public static void main(String[] args) {
        GenerateRandomNumber generateRandomNumber = new GenerateRandomNumber();
        String answerNum = generateRandomNumber.random();
        Answer answer = new Answer(answerNum);
        Game game = new Game(answer);
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要猜的数字");

        while (game.getStatus()==null||game.getStatus().equals(GameStatus.continued)){
            String userInput = sc.nextLine();
            userInput.trim();
            String result = game.play(userInput);
            System.out.println(result);
        }
        sc.close();
        if (game.getStatus().equals(GameStatus.succesed)){
            System.out.println("you win");
        }
        if (game.getStatus().equals(GameStatus.failed)){
            System.out.println("you lose");
        }
    }

    public void init(){
        gameTimes = 0;
    }


    public String play(String userAnswer) {
        gameTimes++;
        answer.setUserInput(userAnswer);
        String checkResult = answer.check();
        if (checkResult.equals("4A0B")){
            status = GameStatus.succesed;
        }else if (gameTimes==6){
            status = GameStatus.failed;
        }else {
            status = GameStatus.continued;
        }
        return checkResult;
    }


    public Answer getAnswer() {
        return answer;
    }

    public void setAnswer(Answer answer) {
        this.answer = answer;
    }

    public int getGameTimes() {
        return gameTimes;
    }

    public void setGameTimes(int gameTimes) {
        this.gameTimes = gameTimes;
    }

    public GameStatus getStatus() {

        return status;
    }

    public void setStatus(GameStatus status) {
        this.status = status;
    }
}
